﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AMS.DataAccess.BO;
using AMS.DataAccess.Interfaces;
using AMS.DataAccess.Trademark.BO;
using AMS.DataAccess.Trademark.Interfaces;

public partial class Customer_ucEditContactor : UCBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Utils.ParseIntQueryString(Request, "caseid", ref caseId);
        if (caseId != 0)
        {
            CaseID = caseId;
        }

        if (!IsPostBack)
        {
            BindProvinceCityDropdownLists();
        }
    }

    #region Properties
    int caseId;
    private bool _commandButtonsVisible = true;
    private int ContactorID
    {
        get { return ViewState["ContactorID"] == null ? 0 : (int)ViewState["ContactorID"]; }
        set { ViewState["ContactorID"] = value; }
    }

    private int CaseID
    {
        get { return ViewState["CaseID"] == null ? 0 : (int)ViewState["CaseID"]; }
        set { ViewState["CaseID"] = value; }
    }

    /// <summary>
    /// default value is add
    /// </summary>
    private string CurrentAction
    {
        get { return ViewState["CurrentAction"] == null ? "add" : (string)ViewState["CurrentAction"]; }
        set { ViewState["CurrentAction"] = value; }
    }
    public bool CommandButtonsVisible
    {
        set { _commandButtonsVisible = value; }
        get { return _commandButtonsVisible; }
    }
    #endregion

    #region Events
    public event EventHandler ContactorAdded;
    protected void OnContactorAdded()
    {
        if (ContactorAdded != null)
        {
            ContactorAdded(this, EventArgs.Empty);
        }
    }
    #endregion

    #region Bind Dropdown List
    void BindProvinceCityDropdownLists()
    {
        IProvinceCityManager pc = ManagerFactory.CreateProvinceManager();
        List<ProvinceCityInfo> provinces = pc.GetProvinces(CallContext);
        drpProvince.DataSource = provinces;
        drpProvince.DataTextField = "ProvinceName";
        drpProvince.DataValueField = "ProvinceID";
        drpProvince.DataBind();

        if (drpProvince.Items.Count > 0)
        {
            BindCities(drpProvince.Items[0].Value);
        }
    }

    private void BindCities(string provinceId)
    {
        IProvinceCityManager mgr = ManagerFactory.CreateProvinceManager();
        List<ProvinceCityInfo> cities = mgr.GetCitiesByProvince(CallContext, provinceId);
        if (cities.Count == 0)
        {
            cities.Add(new ProvinceCityInfo(drpProvince.SelectedValue, drpProvince.Text));
        }

        drpCity.DataSource = cities;
        drpCity.DataTextField = ProvinceCityInfo.NameFieldName;
        drpCity.DataValueField = ProvinceCityInfo.IDFieldName;
        drpCity.DataBind();
    }

    protected void drpProvince_DataBound(object sender, EventArgs e)
    {
        if (drpProvince.Items.Count > 0)
        {
            BindCities(drpProvince.SelectedValue);
        }
    }

    protected void drpProvince_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindCities(drpProvince.SelectedValue);
    }
    #endregion

    public void EditContactor(ContactorInfo contactor)
    {
        txtContactorAddress.Text = contactor.Address;
        txtContactorFax.Text = contactor.Fax;
        txtContactorName.Text = contactor.Name;
        txtContactorZipcode.Text = contactor.Zipcode;
        txtEmail.Text = contactor.Email;
        txtMobile.Text = contactor.Mobile;
        txtTel.Text = contactor.Tel;
        Utils.SelectDropDownList(drpContactorSubfix, contactor.Sex);
        if (Utils.SelectDropDownList(drpProvince, contactor.ProvinceId.ToString()))
        {
            BindCities(drpProvince.SelectedValue);
            Utils.SelectDropDownList(drpCity, contactor.CityId.ToString());
        }

        ContactorID = contactor.AID;
        CurrentAction = "edit";
    }

    public bool RetriveContactor(ContactorInfo contactor)
    {
        if (!ValidateContactor())
        {
            return true;
        }

        contactor.Address = txtContactorAddress.Text;
        contactor.Fax = txtContactorFax.Text;
        contactor.Name = txtContactorName.Text;
        contactor.Zipcode = txtContactorZipcode.Text;
        contactor.Mobile = txtMobile.Text;
        contactor.Tel = txtTel.Text;
        contactor.Email = txtEmail.Text;
        contactor.Sex = drpContactorSubfix.Text;
        contactor.ProvinceId = int.Parse(drpProvince.SelectedValue);
        contactor.CityId = int.Parse(drpCity.SelectedValue);
        return true;
    }

    bool ValidateContactor()
    {
        // TODO: add validate code rere
        return true;
    }

    public void Reset()
    {
        foreach (Control ctrl in Controls)
        {
            if (ctrl is TextBox)
            {
                ((TextBox)ctrl).Text = string.Empty;
            }
            else if (ctrl is DropDownList)
            {
                ((DropDownList)ctrl).SelectedIndex = -1;
            }
        }

        BindCities(drpProvince.SelectedValue);

        ContactorID = 0;
        CurrentAction = null;
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        CaseContactorInfo contactor = null;

        // TODO: validate the applicant
        IContactorManager contactorMgr = ManagerFactory.CreateContactorManager();
        ICaseContactorManager caseContactorMgr = ManagerFactory.CreateCaseContactorManager();
        ICaseBasicInfoManager caseMgr = ManagerFactory.CreateCaseBasicInfoManager();
        List<ContactorInfo> contactors = new List<ContactorInfo>();
        if (CurrentAction == "add")
        {
            CaseBasicInfo caseInfo = caseMgr.GetCaseBasicInfo(CallContext, CaseID);
            contactor = new CaseContactorInfo();
            RetriveContactor(contactor);
            contactor.CaseID = CaseID;
            contactor.CustomerID = caseInfo.CustomerID;

            contactorMgr.AddContactor(CallContext, contactor);
            caseContactorMgr.AddCaseContactor(CallContext, contactor);
            OnContactorAdded();
            MsgInfo("新建联系人成功。");
        }
        else
        {
            contactor = caseContactorMgr.GetCaseContactor(CallContext, CaseID, ContactorID);
            if (contactor != null)
            {
                RetriveContactor(contactor);
                contactors.Add(contactor);
                contactorMgr.UpdateContactors(CallContext, contactors);
                OnContactorAdded();
                MsgInfo("已更新联系人信息：" + contactor.Name);
            }
            else
            {
                MsgInfo("联系信息不存在，可能已经被删除。");
            }
        }
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        Reset();
    }
}
